<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="内容" prop="content">
        <el-input
          v-model="queryParams.content"
          placeholder="请输入要搜索的内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="帖子状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择帖子状态" clearable size="small">
          <el-option
            v-for="dict in statusOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="商品所属分类" prop="classifyId">
        <el-select v-model="queryParams.classifyId" placeholder="请选择商品所属分类" clearable size="small">
          <el-option label="请选择所属分类" value="" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['posts:post:add']"
        >新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['posts:post:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['posts:post:remove']"
        >删除</el-button>
      </el-col> -->

	  <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="postList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
     <!-- <el-table-column label="自增主键" align="center" prop="id" /> -->
      <el-table-column label="用户id" align="center" prop="userId" />
      <el-table-column label="内容" align="center" width="220px">
        <template slot-scope="scope">
          <el-tooltip class="item" effect="dark" :content="scope.row.content" placement="bottom">
               <div style="overflow:hidden;text-overflow: ellipsis;-webkit-line-clamp: 2;display: -webkit-box;-webkit-box-orient: vertical;">
                 {{scope.row.content}}
               </div>
          </el-tooltip>
        </template>
      </el-table-column>
      <el-table-column label="图片集合" align="center" width="120px">
        <template slot-scope="scope">
          <el-image
            style="width: 80px; height: 80px"
            :src="scope.row.imgUrls[0]"
            :preview-src-list="scope.row.imgUrls">
            <div slot="error" class="image-slot">
              <i class="el-icon-picture-outline"></i>
            </div>
          </el-image>
        </template>
      </el-table-column>
      <el-table-column label="帖子评论总数" align="center" prop="commentCount" />
      <el-table-column label="帖子状态" align="center" prop="status" :formatter="statusFormat" />
      <el-table-column label="商品原总价" align="center" prop="amountOfOriginalPrice" />
      <el-table-column label="商品现总价" align="center" prop="amountOfCurrentPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" width="150">
        <template slot-scope="scope">
          <!-- <el-button
            size="mini"
            type="primary"
            style="padding: 4px 6px;"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['posts:post:edit']"
          >修改</el-button> -->
          <el-button
            size="small"
            v-if="scope.row.status==0"
            style="padding: 4px 6px;background-color: #e65959;"
            type="danger"
            @click="open2(scope.row.id, 0)"
            v-hasPermi="['member:member:remove']"
          >下架</el-button>
          <el-button
            size="small"
            v-else
            style="padding: 4px 6px;"
            type="success"
            @click="open2(scope.row.id, 1)"
            v-hasPermi="['member:member:remove']"
          >恢复</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改失物招领、拼车服务、二手交易帖子对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="图片集合" prop="imgUrls">
          <el-input v-model="form.imgUrls" placeholder="请输入图片集合" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="form.status" placeholder="请选择状态">
            <el-option
              v-for="dict in statusOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品原总价" prop="amountOfOriginalPrice">
          <el-input v-model="form.amountOfOriginalPrice" placeholder="请输入商品原总价" />
        </el-form-item>
        <el-form-item label="商品现总价" prop="amountOfCurrentPrice">
          <el-input v-model="form.amountOfCurrentPrice" placeholder="请输入商品现总价" />
        </el-form-item>
        <el-form-item label="商品所属分类" prop="classifyId">
          <el-select v-model="form.classifyId" placeholder="商品所属分类">
            <el-option label="请选择字典生成" value="" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listPost, getPost, delPost, addPost, updatePost, exportPost } from "@/api/posts/post";
import Editor from '@/components/Editor';

export default {
  name: "Post",
  components: {
    Editor,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 失物招领、拼车服务、二手交易帖子表格数据
      postList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 0-正常; 1-拉黑;字典
      statusOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userId: null,
        content: null,
        status: null,
        classifyId: null,
        type: 3,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("post_status").then(response => {
      this.statusOptions = response.data;
    });
  },
  methods: {
    /** 查询失物招领、拼车服务、二手交易帖子列表 */
    getList() {
      this.loading = true;
      listPost(this.queryParams).then(response => {
        let list = response.rows
        for(let i=0; i<list.length; i++){
          list[i].imgUrls = list[i].imgUrls != null ? list[i].imgUrls.split(',') : []
        }
        this.postList = list;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 0-正常; 1-拉黑;字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        title: null,
        content: null,
        imgUrls: null,
        commentCount: null,
        type: null,
        status: null,
        amountOfOriginalPrice: null,
        amountOfCurrentPrice: null,
        createTime: null,
        updateTime: null,
        score: null,
        classifyId: null,
        isFinishDeal: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加帖子";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getPost(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改帖子";
      });
    },
    open2(id, sign) {
      let tip
      if(sign == 0){
        this.form.status = 1
        tip = '下架'
      }else {
        this.form.status = 0
        tip = '恢复'
      }
      this.$confirm(`您是否确定${tip}该商品呢?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.reset();
        this.form.id = id
        updatePost(this.form).then(response => {
          this.$message({type: 'success',message: `${tip}商品成功`})
          this.getList();
        })
      }).catch(() => {
        this.$message({type: 'info',message: '已取消'})
      })
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updatePost(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPost(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除失物招领、拼车服务、二手交易帖子编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delPost(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有失物招领、拼车服务、二手交易帖子数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportPost(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
}
</script>

<style lang="scss">
  .el-tooltip__popper.is-dark{
    width: 400px;
  }
</style>
